// 引入接口文件
import {getRandom,changeMobile} from './changmobileapplyServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
import config from '../../../config/config'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"
// 初始默认状态
const defultState = {
  loading: false,
  btnLoading: false,
  detail:{
    flag_chnl: "H5"
  },
  data:{}
}
// 声明module
const tmpModule = {
  namespace: 'changmobileapplyModel',
  state: cloneDeep(defultState),
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        if('' + tmpModule.pathname === '' + pathname){
          dispatch({ type: 'updateStore', payload: cloneDeep(defultState) });
          // dispatch({ type: 'typeList', payload: {} });
        }
      })
    },
  },
  // 方法列表
  effects: {
    *Random({payload},{put,call,select}){
      try{
        console.log(payload)
        const { data={}, success } = yield call(getRandom,{...payload.detail});
        
        if(success){
          var random = encodeURI(JSON.stringify({
            license: data.license,
            rsaPublicKey: data.rsa_public_content,
            passGuardId: data.map_arr,
            randomValue: data.random_value,
            randomKey: data.random_key
          }))
          // console.log(config.baseUrl)
          const baseUrl=config.baseUrl
          const url=baseUrl+`h5/pwd/index.html#pwdRandom=`+random
          window.open(url)
          yield put({ type: 'updateStore',payload:{data}})

        }

        
      }catch(e){
        console.log(e)
      }
    },
    *changePhone({payload},{put,call,select}){
      let defultState = yield select(state => state[tmpModule.namespace])
      console.log(defultState)
      let {random_key}=defultState.data
      const { success } = yield call(changeMobile,{...defultState.detail,random_key});
      if(success){
        message.success('修改成功');
      }
    },


    *typeList({payload},{ put, call, select }){
      try{
        const { rows, success } = yield call(getType);
        let defultState = yield select(state => state[tmpModule.namespace])
        if(success) {
          // console.log('data',rows)
          // message.success('删除成功')
          // message.success('删除成功',()=>{
          //   defultState.biz.refreshListDatas(true)
          // })
          yield put({ type: 'updateStore',payload:{typeOption:rows}})
        }else{

        }
      }catch(e){

      }
    },
    
  },
  
 

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return { ...preState, ...action.payload };
    }
  }
}

export default tmpModule
